﻿using System;
using System.Collections.Generic;

using System.Text;
using System.Windows.Forms;
using Itegracion.Framework.Model;
using System.Data;

namespace Itegracion.Framework.Forms
{
    public class Helper
    {
        private static Helper instance;
        public static Helper Instance{
            get
            {
                if (instance == null)
                    instance = new Helper();
                return instance;
            }
        }

        private string assembly, type, method;
        private List<Element> input;
        public Helper In(string assembly, string type, string method, params object[] controls)
        {
            this.assembly = assembly;
            this.type = type;
            this.method = method;

            input = new List<Element>();
            foreach (object item in controls)
            {
                input.Add(Element.Parse(item));
            }

            return this;
        }

        public Helper Out(params object[] controls)
        {
            ProxyCode code;
            List<Element> output = Proxy.Invoke(assembly, type, method, input, out code);

            for (int i = 0; i < output.Count; i++)
            {
                if(i < controls.Length)
                    LoadFrom(controls[i], output[i]);
            }
            input = null;
            return this;
        }

        private void LoadFrom(object control, Element element)
        {
            if (control is TextBox)
                (control as TextBox).Text = "" + element.Value;

            if (control is Label)
                (control as Label).Text = "" + element.Value;

            if (control is ComboBox)
                if (element.Value is DataTable)
                {
                    (control as ComboBox).DataSource = element.Value;
                    (control as ComboBox).ValueMember = "id";
                    (control as ComboBox).DisplayMember = "nombre";
                }
                else
                    (control as ComboBox).SelectedValue = element.Value;
            if (control is DataGridViewComboBoxColumn)
            {
                (control as DataGridViewComboBoxColumn).DataSource = element.Value;
                (control as DataGridViewComboBoxColumn).ValueMember = "id";
                (control as DataGridViewComboBoxColumn).DisplayMember = "nombre";
            }
            if (control is DateTimePicker)
                (control as DateTimePicker).Value = Convert.ToDateTime(element.Value);

            if (control is DataGridView)
            {
                (control as DataGridView).DataSource = element.Value;
            }
            if (control is DataTable)
            {
                (control as DataTable).TableName = element.Name;
                (control as DataTable).Merge(element.Value as DataTable, false);
            }
            if (control is Element)
            {
                (control as Element).Name = element.Name;
                (control as Element).Value = element.Value;
            }
            if (control is List<object>)
            {
                List<object> ctrls = (List<object>)control;
                for (int i = 0; i < ctrls.Count; i++)
                {
                    object o = ctrls[i];
                    LoadFrom(o, element);
                }
            }
        }
    }

    public class Element {
        public Element(){}
        public Element(string name, object value) {
            this.Name = name;
            this.Value = value;
        }

        public string Name { get; set; }
        public object Value { get; set; }

        public static Element Parse(object control) { 
            Element rpta = new Element();
            if (control is Element)
            {
                rpta.Name = (control as Element).Name;
                rpta.Value = (control as Element).Value;
            }
            if (control is TextBox){
                rpta.Name = (control as TextBox).Name;
                rpta.Value = (control as TextBox).Text;
            }
            if (control is ComboBox){
                rpta.Name = (control as ComboBox).Name;
                rpta.Value = (control as ComboBox).SelectedValue;
            }   
            if (control is DateTimePicker){
                rpta.Name = (control as DateTimePicker).Name;
                rpta.Value = (control as DateTimePicker).Value;
            }
            if (control is DataGridView){
                rpta.Name = (control as DataGridView).Name;
                rpta.Value = (control as DataGridView).DataSource;
            }
            if (control is DataGridViewCell){
                rpta.Name = (control as DataGridViewCell).ColumnIndex.ToString();
                rpta.Value = (control as DataGridViewCell).Value;
            }
            return rpta;
        }

        
    }
}
